//
//  MTCompationInfoController.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/5.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTCompationInfoController.h"
#import "MTUploadAvatarTipView.h"
#import "MTMainViewController.h"

@interface MTCompationInfoController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,MTUploadAvatarTipViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nametextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordtextField;

@property (weak, nonatomic) IBOutlet UILabel *titleLable;


@property(nonatomic,strong) MTUploadAvatarTipView *tipView;


@property (weak, nonatomic) IBOutlet UIButton *imageButton;
@property (weak, nonatomic) IBOutlet UIButton *manButton;
@property (weak, nonatomic) IBOutlet UIButton *womanButton;

@property (weak, nonatomic) IBOutlet UILabel *manLable;
@property (weak, nonatomic) IBOutlet UILabel *womanLable;

@property (weak, nonatomic) IBOutlet UIButton *sureButton;

@property (weak, nonatomic) IBOutlet UILabel *uploadImageLable;

@property(nonatomic,strong) UIButton *switchButton;

@property(nonatomic,strong) UIImage *selectImage;
@property(nonatomic,copy) NSString *imageUrl;
@property(nonatomic,copy) NSString *gander;


@end

@implementation MTCompationInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gander = @"1";
    self.manLable.text = NSLTLocalizedString(@"personInfo.male", nil);
    self.womanLable.text = NSLTLocalizedString(@"personInfo.famale", nil);
    self.titleLable.text = NSLTLocalizedString(@"register.wanshanxingxu", nil);
    [self.sureButton setTitle:NSLTLocalizedString(@"main.sure", nil) forState:UIControlStateNormal];
    self.uploadImageLable.text = NSLTLocalizedString(@"register.uploadimage", nil);

    self.manButton.selected = YES;
    _switchButton = [UIButton new];
    [_switchButton setImage:[UIImage imageNamed:@"fillup_password_hide"] forState:UIControlStateNormal];
    [_switchButton setImage:[UIImage imageNamed:@"fillup_password_show"] forState:UIControlStateSelected];
    _switchButton.size = CGSizeMake(25, 25);
    [_switchButton addTarget:self action:@selector(switchClick) forControlEvents:UIControlEventTouchUpInside];
    self.passwordtextField.rightView = _switchButton;
    self.passwordtextField.rightViewMode = UITextFieldViewModeAlways;
}

-(void)registerClick
{
   [MBProgressHUD showProcessHud:NSLTLocalizedString(@"main.loding", nil) toView:self.view];
    if(self.selectImage)
    {
        NSData *data = UIImageJPEGRepresentation(self.selectImage, 0.1);
        [[MTHttpTool shareTool] uploadImageData:data progress:^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
        } success:^(NSString *fileUrl, LTFileTokenModel *model) {
            self.imageUrl = fileUrl;
            [[MTUserApiManager manager] registerUser:self.phone countryCode:self.contryCode verifyCode:self.verCode type:@"1" avatar:self.imageUrl name:self.nametextField.text password:self.passwordtextField.text gender:@"1" completion:^(MTUserModel *user, MTError *mterror) {
                [MBProgressHUD hideHUDForView:self.view];
                if(mterror == nil)
                {
                    [[LTClient sharedClient] loginWithUser:[LTSDKHelper usertransformMTUser:user]];
                    MTMainViewController *mainVc = [[MTMainViewController alloc] init];
                    [UIApplication sharedApplication].keyWindow.rootViewController = mainVc;
                    NSLog(@"注册成功");
                }else
                {
                    [MBProgressHUD showError:mterror.errorDescription];
                }
            }];
        } failer:^(MTError *lterror) {
            [MBProgressHUD hideHUDForView:self.view];
            [MBProgressHUD showError:lterror.errorDescription];
        }];
    }else
    {
        [[MTUserApiManager manager] registerUser:self.phone countryCode:self.contryCode verifyCode:self.verCode type:@"1" avatar:self.imageUrl name:self.nametextField.text password:self.passwordtextField.text gender:@"1" completion:^(MTUserModel *user, MTError *mterror) {
            [MBProgressHUD hideHUDForView:self.view];
            if(mterror == nil)
            {
                [[LTClient sharedClient] loginWithUser:[LTSDKHelper usertransformMTUser:user]];
                MTMainViewController *mainVc = [[MTMainViewController alloc] init];
                [UIApplication sharedApplication].keyWindow.rootViewController = mainVc;
                NSLog(@"注册成功");
            }else
            {
                [MBProgressHUD showError:mterror.errorDescription];
            }
        }];
    }

    
    
    
    
}



-(void)buttonClick:(NSInteger)type
{
    if (type == 1) //skip
    {
        [self registerClick];
        
    }else{
        [self selectClick:nil];
    }
    
    
}

-(void)switchClick
{
    if (self.switchButton.selected == NO)
    {
        self.switchButton.selected = YES;
        NSString *tempPwdStr = self.passwordtextField.text;
        self.passwordtextField.text = @""; // 这句代码可以防止切换的时候光标偏移
        self.passwordtextField.secureTextEntry = NO;
        self.passwordtextField.text = tempPwdStr;
    }else
    {
        self.switchButton.selected = NO;
        NSString *tempPwdStr = self.passwordtextField.text;
        self.passwordtextField.text = @"";
        self.passwordtextField.secureTextEntry = YES;
        self.passwordtextField.text = tempPwdStr;
    }
}

- (IBAction)sureClick:(UIButton *)sender {
    if(_nametextField.text.length == 0)
    {
        [self showHint:NSLTLocalizedString(@"register.tip3", @"请输入昵称") ];
        return;
    }
    if(_nametextField.text.length > 50)
    {
        UIAlertController *controller = [UIAlertController alertControllerWithTitle:NSLTLocalizedString(@"register.tip6","") message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"main.ok", "") style:UIAlertActionStyleDefault handler:nil];
        [controller addAction:action];
        [self presentViewController:controller animated:YES completion:nil];
        return;
    }
    if(_passwordtextField.text.length == 0)
    {
        [self showHint:NSLTLocalizedString(@"register.tip4", @"请输入密码")];
        return;
    }
    if(_passwordtextField.text.length < 6 || _passwordtextField.text.length > 25)
    {
        UIAlertController *controller = [UIAlertController alertControllerWithTitle:NSLTLocalizedString(@"register.tip7","") message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"main.ok", "") style:UIAlertActionStyleDefault handler:nil];
        [controller addAction:action];
        [self presentViewController:controller animated:YES completion:nil];
        return;
    }
    if(self.selectImage == nil)
    {
        [self.tipView show];
    }else{
        [self registerClick];
    }
    
}

-(MTUploadAvatarTipView *)tipView
{
    if(_tipView == nil)
    {
        _tipView = [MTUploadAvatarTipView uploadavatarTipView];
        _tipView.deleagte = self;
    }
    return _tipView;
}

#pragma mark - 照相
- (void)takePhoto {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    
    
}

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *originalimage = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *editImage = [info objectForKey:UIImagePickerControllerEditedImage];
    if(editImage != nil)
    {
        self.selectImage = editImage;
    }else
    {
        self.selectImage = originalimage;
    }
    [self.imageButton setBackgroundImage:self.selectImage forState:UIControlStateNormal];
}



#pragma mark - 选择图片
- (void)pushTZImagePickerController {
    
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    
    
}

- (IBAction)selectClick:(UIButton *)sender {
//    [self.selectphotoView show];
    
    UIAlertController *alterVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"personInfo.takePhoto", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self takePhoto];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"personInfo.selectPhoto", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self pushTZImagePickerController];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"main.cancle", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alterVc addAction:action1];
    [alterVc addAction:action2];
    [alterVc addAction:action3];
    [self presentViewController:alterVc animated:YES completion:nil];
    
}

- (IBAction)manClick:(UIButton *)sender {
    
    if (sender.selected == NO)
    {
        sender.selected = YES;
        self.womanButton.selected = NO;
        self.gander = @"1";
    }else
    {
        
    }

    
}
- (IBAction)womanClick:(UIButton *)sender {
    if (sender.selected == NO)
    {
        sender.selected = YES;
        self.manButton.selected = NO;
        self.gander = @"2";
    }else
    {
       
    }
}



@end
